"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MatchUserDao = void 0;
const DbConfig_1 = require("../../src/config/DbConfig");
const GameConfig_1 = require("../../src/config/GameConfig");
const DaoBase_1 = require("../../src/engine/dao/DaoBase");
const DateUtil_1 = require("../../src/engine/utils/DateUtil");
const Logger_1 = require("../../src/engine/utils/Logger");
const MysqlManager_1 = require("../../src/engine/utils/MysqlManager");
class MatchUserDao extends DaoBase_1.DaoBase {
    static is_math_user_exist(userid, gameType) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select userid from " + this.DB_NAME + "." + this.Tb_Name + " where `userid`=? and `gameType`=?";
            let sqlParams = [userid, gameType];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static create_match_user(userid, gameType) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = 'insert  into ' + this.Tb_Name + '(userid, last_time, gameType, turn,score,max_score) VALUES(?,?,?,?,?,?) ;';
            let timeStr = DateUtil_1.default.formatDateStr(DateUtil_1.default.now());
            let sqlParams = [userid, timeStr, gameType, 0, 0, 0];
            let rows = yield client.queryPromise(sql, sqlParams);
            if (rows && rows["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static get_match_user(userid, gameType) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select * from t_match_users WHERE userid = ? and gameType=?";
            let sqlParams = [userid, gameType];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results.length) {
                return results[0];
            }
        });
    }
    static update_match_enroll(userid, gameType) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = 'UPDATE t_match_users SET last_time = ? WHERE userid = ? and gameType=?';
            let timeStr = DateUtil_1.default.formatDateStr(DateUtil_1.default.now());
            let sqlParams = [timeStr, userid, gameType];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static update_match_turn(userid, gameType, turn) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = 'UPDATE t_match_users SET turn = ? WHERE userid = ? and gameType=?';
            let sqlParams = [turn, userid, gameType];
            let results = yield client.queryPromise(sql, sqlParams);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static endMatchGame(userid, gameType, score, max_score, matchTurn) {
        return __awaiter(this, void 0, void 0, function* () {
            if (matchTurn >= GameConfig_1.GameConfig.Max_Match_Turn) {
                score = 0;
                matchTurn = 0;
            }
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = 'UPDATE t_match_users SET score = ?, `max_score`=?, `turn`=? where `userid`=? and `gameType`=?';
            let sqlParams = [score, max_score, matchTurn, userid, gameType];
            let results = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("endMatchGame===", sql, sqlParams, results);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static clearMatchDaily(now) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = 'UPDATE t_match_users SET score = 0, `max_score`=0, `turn`=0 ';
            let results = yield client.queryPromise(sql);
            if (results && results["affectedRows"] > 0) {
                return true;
            }
            else {
                return false;
            }
        });
    }
    static getTop(gameType, topNum) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let startIndex = 0;
            let sql = "select t_match_users.userid,t_match_users.max_score, t_users.nick_name,t_users.headimg, t_users.lv  from t_match_users left join t_users on t_match_users.userid=t_users.userid   where t_match_users.gameType=? order by max_score desc limit ?,?";
            let sqlParams = [gameType, startIndex, topNum];
            let rows = yield client.queryPromise(sql, sqlParams);
            Logger_1.Logger.log("getTop=", rows);
            if (rows && rows.length) {
                return rows;
            }
            return [];
        });
    }
    static getMyTop(gameType, userid) {
        return __awaiter(this, void 0, void 0, function* () {
            let client = yield MysqlManager_1.MysqlManager.instance.getDb(this.DB_NAME);
            let sql = "select t_match_users.userid,t_match_users.max_score, t_users.nick_name,t_users.headimg, t_users.lv from t_match_users left join t_users on t_match_users.userid=t_users.userid    where t_match_users.gameType=? and t_match_users.userid=?";
            let sqlParams = [gameType, userid];
            let rows = yield client.queryPromise(sql, sqlParams);
            if (rows && rows.length) {
                return rows[0];
            }
        });
    }
}
exports.MatchUserDao = MatchUserDao;
MatchUserDao.DB_NAME = DbConfig_1.DbConfig.Db_Name.db_admin;
MatchUserDao.Tb_Name = "t_match_users";
